package com.ds.infrastructure.hyperspace.container.admin;

import com.ds.infrastructure.hyperspace.common.admin.AdminConstants;
import com.ds.infrastructure.hyperspace.container.admin.processor.*;
import com.ds.infrastructure.hyperspace.container.internal.NettyContainer;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.http.HttpServerCodec;

import java.util.HashMap;
import java.util.Map;

/**
 * @author  Weishaoying
 * @date 2019/6/26 上午11:55
 */
public class AdminServerChannelInitializer extends ChannelInitializer<Channel> {

    private final NettyContainer container;

    private final Map<String, Processor> adminProcessorMap;

    public AdminServerChannelInitializer(NettyContainer container) {
        this.container = container;
        this.adminProcessorMap = new HashMap<>();
        this.adminProcessorMap.put(AdminConstants.APPLY_CONFIG, new ApplyConfigProcessor());
        this.adminProcessorMap.put(AdminConstants.BLACK_HOLE, new BlackHoleProcessor());
        this.adminProcessorMap.put(AdminConstants.GZS_STATUS,
                new GzsStatusProcessor(container.getGzsService()));
        this.adminProcessorMap.put(AdminConstants.OPENTRACING_CONFIG, new OpentracingConfigProcessor());
    }

    @Override
    protected void initChannel(Channel channel) throws Exception {
        ChannelPipeline channelPipeline = channel.pipeline();
        channelPipeline.addLast(new HttpServerCodec());
        channelPipeline.addLast(new AdminServerHandler(container, adminProcessorMap));
    }
}
